package com.moonLight.weblog.web.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.moonLight.weblog.common.domain.dos.WikiCatalogDO;
import com.moonLight.weblog.common.domain.dos.WikiDO;
import com.moonLight.weblog.common.domain.mapper.WikiCatalogMapper;
import com.moonLight.weblog.common.domain.mapper.WikiMapper;
import com.moonLight.weblog.common.enums.WikiCatalogLevelEnum;
import com.moonLight.weblog.common.utils.Response;
import com.moonLight.weblog.web.model.vo.article.FindPreNextArticleRspVO;
import com.moonLight.weblog.web.model.vo.wiki.*;
import com.moonLight.weblog.web.service.WikiService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Service
public class WikiServiceImpl implements WikiService {
    @Autowired
    private WikiMapper wikiMapper;
    @Autowired
    private WikiCatalogMapper wikiCatalogMapper;
    @Override
    public Response findWikiList() {
        //返回给前段的数据
        List<FindWikiListRspVO> vos = Lists.newArrayList();
        //查询已经发布的知识库
        List<WikiDO> wikiDOS = wikiMapper.selectPublished();

        //Do 转 Vo 顺便给每个知识库设置第一篇文章
        if (!CollectionUtils.isEmpty(wikiDOS)) {

            vos = wikiDOS.stream().map(wikiDO -> {
                FindWikiListRspVO vo = FindWikiListRspVO.builder()
                        .id(wikiDO.getId())
                        .title(wikiDO.getTitle())
                        .cover(wikiDO.getCover())
                        .summary(wikiDO.getSummary())
                        .isTop(wikiDO.getWeight() > 0)
                        .build();
                return vo;
            }).collect(Collectors.toList());
            //设置每一个知识库的第一篇文章
            vos.forEach(vo -> {
                WikiCatalogDO wikiCatalogDO = wikiCatalogMapper.selectFirstArticleId(vo.getId());
                vo.setFirstArticleId(Objects.nonNull(wikiCatalogDO) ? wikiCatalogDO.getArticleId() : null);
            });
        }

        return Response.success(vos);
    }

    @Override
    public Response findWikiCatalogList(FindWikiCatalogListReqVO findWikiCatalogListReqVO) {

        //拿到知识库的ID
        Long id = findWikiCatalogListReqVO.getId();
        //将目录中的所用属于该ID下的数据拿到
        List<WikiCatalogDO> catalogDOS = wikiCatalogMapper.selectByWikiId(id);

        List<WikiCatalogDO> collect_one = catalogDOS.stream().filter(catalogDO -> catalogDO.getLevel() == WikiCatalogLevelEnum.ONE.getValue()).collect(Collectors.toList());

        List<WikiCatalogDO> collect_two = catalogDOS.stream().filter(catalogDO -> catalogDO.getLevel() == WikiCatalogLevelEnum.TWO.getValue()).collect(Collectors.toList());


        //先按照一级目录创建 之后添加二级目录
        List<FindWikiCatalogListRspVO> CatalogOne = collect_one.stream().map(catalogDO ->
                FindWikiCatalogListRspVO.builder()
                        .id(catalogDO.getId())
                        .articleId(null)
                        .title(catalogDO.getTitle())
                        .level(WikiCatalogLevelEnum.ONE.getValue())
                        .children(null)
                        .build()
        ).collect(Collectors.toList());

        CatalogOne.forEach(one -> {
            List<FindWikiCatalogListRspVO> CatalogTwo = collect_two.stream()
                    .filter(catalogDO -> Objects.equals(catalogDO.getParentId(), one.getId()))
                    .map(catalogDO ->
                            FindWikiCatalogListRspVO.builder()
                                    .id(catalogDO.getId())
                                    .articleId(catalogDO.getArticleId())
                                    .title(catalogDO.getTitle())
                                    .level(catalogDO.getLevel())
                                    .children(null)
                                    .build()
                    ).collect(Collectors.toList());
            one.setChildren(CatalogTwo);

        });
        return Response.success(CatalogOne);
    }

    @Override
    public Response findArticlePreNext(FindWikiArticlePreNextReqVO findWikiArticlePreNextReqVO) {
        Long wikiId = findWikiArticlePreNextReqVO.getId();
        Long articleId = findWikiArticlePreNextReqVO.getArticleId();
        FindWikiArticlePreNextRspVO vo = new FindWikiArticlePreNextRspVO();

        WikiCatalogDO preArticleCatalogDO = wikiCatalogMapper.selectPreArticle(wikiId, articleId);
        WikiCatalogDO nextArticleCatalogDO = wikiCatalogMapper.selectNextArticle(wikiId, articleId);

        //Do 转 Vo
        if (Objects.nonNull(preArticleCatalogDO)) {
            vo.setPreArticle(FindPreNextArticleRspVO.builder()
                    .articleId(preArticleCatalogDO.getArticleId())
                    .articleTitle(preArticleCatalogDO.getTitle())
                    .build());
        }
        if (Objects.nonNull(nextArticleCatalogDO)){
            vo.setNextArticle(FindPreNextArticleRspVO.builder()
                    .articleId(nextArticleCatalogDO.getArticleId())
                    .articleTitle(nextArticleCatalogDO.getTitle())
                    .build());
        }


        return Response.success(vo);
    }


}
